import waves from "@/directive/waves";
import Pagination from "@/components/Pagination";
import { alertMessage, isValidateNull, showAlertMessage, alertHtmlWithCallback } from "@/utils/utils";
import { mapGetters } from 'vuex';
import { getDictionaryTypeApi, deleteSelectedDicTypeApi } from "./dictionary-management.service";
import CreateDictionaryType from './components/create-dictionary-type/create-dictionary-type';
import { getUserInfo } from "@/utils/auth";
export default {
  name: 'dictionary-management',
  components: { Pagination, CreateDictionaryType },
  directives: { waves },
  data() {
    return {
      userInfo: getUserInfo(),
      loading: true, // table loading
      dialogTitle: '', // 新建用户、编辑用户
      // 筛选
      searchForm: {}, // 搜索内容
      confirmSearchForm: {}, // 确认提交搜索内容
      // table列表分页
      checkedRows: [], // 批量选中的数据
      selectRow: {}, // 列表选中的行
      data: [], // 列表数据
      pageNum: 1, // 页码
      recordNum: 10, // 条数
      totalNum: 0, // 总数

      createOrEditDialogVisible: false, // 创建/编辑字典类型弹窗
      type: '', // 编辑类型(copy:复制)
      selectDicType: '', // 选中的字典类型
      selectedDataDetail: {}, // 选中用户详情
      editDialogVisible: false,
      editForm: {}, // 表单对象
      editRules: {
        tableName: [{ required: true, trigger: "change", validator: isValidateNull }],
        itemValue: [{ required: true, trigger: "change", validator: isValidateNull }],
        itemName: [{ required: true, trigger: "change", validator: isValidateNull }]
      } // 表单规则
    }
  },
  computed: {
    ...mapGetters([
      'codesCheck'
    ])
  },
  created() {
    this.getDataList();
  },
  methods: {
    setIndex(index) {
      return (this.pageNum - 1) * this.recordNum + index + 1;
    },
    // 获取列表
    getDataList() {
      this.loading = false;
      const params = {
        tableName: this.confirmSearchForm.tableName,
        typeName: this.confirmSearchForm.typeName,
        pageNum: this.pageNum,
        recordNum: this.recordNum
      }
      getDictionaryTypeApi(params).then(res => {
        this.totalNum = Number(res.dataCount);
        this.data = res.data;
        this.loading = false;
      });
    },

    // 打开新建/编辑窗体
    openCreateOrEditDialog(row, type) {
      this.type = type;
      this.selectDicType = row
      this.createOrEditDialogVisible = true;
    },

    // 关闭新建/编辑弹窗
    closeCreateOrEditDialog() {
      this.createOrEditDialogVisible = false;
    },

    // 刷新数据
    reload() {
      this.pageNum = 1;
      this.getDataList();
    },

    // 搜索列表
    search() {
      this.pageNum = 1;
      this.confirmSearchForm = JSON.parse(JSON.stringify(this.searchForm));
      this.getDataList();
    },

    // 刷新列表
    onReset() {
      this.pageNum = 1;
      this.confirmSearchForm = {
        typeName: '',
        tableName: ''
      }
      this.searchForm = JSON.parse(JSON.stringify(this.confirmSearchForm))
      this.getDataList();
    },

    // 处理checkbox选中行数据
    handleSelectionChange(val) {
      this.checkedRows = val;
    },

    // 删除单/多条选中数据
    deleteSelectedData(id, name) {
      // 如果是单条删除
      let ids = id ? [id] : []
      if (ids.length === 0 && this.checkedRows.length === 0) {
        alertMessage('请至少选择一条数据', 'warning');
      } else {
        const message = {
          name: ids.length === 0 ? this.checkedRows.map(v => v.typeName).join('&nbsp;&nbsp;') : name
        }
        const alertMsg = showAlertMessage(`字典名称为:`, message)
        alertHtmlWithCallback(alertMsg, `是否确认删除已选中的${ids.length === 0 ? this.checkedRows.length : 1}条数据`).then(() => {
          if (ids.length === 0) {
            this.checkedRows.forEach(item => {
              ids.push(item.objectId)
            });
          }
          deleteSelectedDicTypeApi(ids).then((res) => {
            alertMessage(res.message);
            this.onReset();
          }).catch(() => { }); // 关闭confirm框的回调，不处理的话会报一个js error
        }).catch(() => { }); // 关闭confirm框的回调，不处理的话会报一个js error
      }
    },

    // 跳转到字典列表
    moveToDictionaryListPage(table) {
      this.$router.push({ path: '/system-management/dictionary-list', query: { table: table } })
    }
  }
}
